<?php
class Detail extends MY_Controller
{
	public function __construct()
	{
		log_message('debug', '开始调用Currency->Detail->__construct接口');
		parent::__construct();
		$this->loadLanguageFile('application/controllers/Currency/');
		log_message('debug', '调用Currency->Detail->__construct接口结束');
	}
	
	
	public function showView()
	{
		$this->load->view('module/Currency/detail.htm');
	}
	
	
	public function getListData()
	{
		log_message('debug', '=====开始调用Currency->Detail->getData接口=====');
		$this->verifyPrivilege();
		try {
			$gameAreaId = $this->param->getParam('gameAreaId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 201, 'invalidErrorCode' => 1));
			$account = $this->param->getParam('account', array('method' => 'string', 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$roleName = $this->param->getParam('roleName', array('method' => 'string', 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$startDateTime = $this->param->getParam('startDateTime', array('method' => 'string', 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$endDateTime = $this->param->getParam('endDateTime', array('method' => 'string', 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$currencyTypeId = $this->param->getParam('currencyTypeId', array('method' => 'int', 'min' => 1, 'max' => 999999, 'emptyErrorCode' => 7001, 'invalidErrorCode' => 1));
			$page = $this->param->getParam('page', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$rows = $this->param->getParam('rows', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			
			if( !$account && !$roleName ) {
				throw new Exception('', 202);
			}
	
			$this->initGameAreaInfo($gameAreaId);
	
			$sql = "
				select
					`userId`
				from
					`t_data_game_account`
				where
					(
						`account` = '{$account}'
						or
						`roleName` = '{$roleName}'
					)
					and
					`gameAreaId` = '{$gameAreaId}'
			";

			if( ($row = $this->gmDatabase->getRow($sql)) === false) {
				throw new Exception('', 204);
			}

			$userId = $row['userId'];
					
			$sql = "
				select
					<selectColumns>
				from
					`t_data_game_currency` a
				left join
					`t_data_game_account` c
				on
					a.`userId` = c.`userId`
				left join
					(
						select
							a.`functionId`,
							a.`function{$this->currLanguage}Name`,
							if(b.`subFunctionId` is null, 0, b.`subFunctionId`) as subFunctionId,
							b.`subFunction{$this->currLanguage}Name`
						from
							`t_cfg_currency_function` a
						left join
							`t_cfg_currency_subfunction` b
						on
							a.`functionId` = b.`functionId`
					) b
				on
					a.`functionId` = b.`functionId`
					and
					a.`subFunctionId` = b.`subFunctionId`
				where
					a.`currencyTypeId` = '{$currencyTypeId}'
			";
			if($account || $roleName){
				$sql .= "
				 		and
				 		(
						c.`account` = '{$account}'
						or
						c.`roleName` = '{$roleName}'
					)
				 ";
			}
			
			if($startDateTime) {
				$sql .= " and a.`operatorDateTime` >= '{$startDateTime}'";
			}
	
			if($endDateTime) {
				$sql .= " and a.`operatorDateTime` <= '{$endDateTime}'";
			}
			$getTotalSql = str_replace('<selectColumns>', "count(*)", $sql);
			
			$this->returnData['data']['total'] = $this->gmDatabase->getColumn($getTotalSql);
			$getDataSql = str_replace('<selectColumns>', "if(b.`function{$this->currLanguage}Name` is null, a.`functionId`, b.`function{$this->currLanguage}Name`) as `functionName`, if(b.`subFunction{$this->currLanguage}Name` is Null, a.`subFunctionId`, b.`subFunction{$this->currLanguage}Name`) as subFunctionName, a.`changeValue`, a.`surplusValue`, a.`operatorDateTime`", $sql);
			if($page && $rows) {
				$startRow = ($page - 1) * $rows;
				$getDataSql .= " limit {$startRow}, {$rows}";
			}
			
			$this->returnData['data']['rows'] = $this->gmDatabase->getArray($getDataSql);
		}
	
		catch(MY_Param_Exception $e) {
			$this->setErrorCode($e->getCode());
		}
	
		catch(MY_Mysql_Exception $e) {
			$this->setErrorCode(1);
		}
	
		catch(Exception $e) {
			$this->setErrorCode($e->getCode());
		}
	
		$this->returnJson();
		log_message('debug', '=====调用Gold->getListData接口结束=====');
	}
}